﻿using MCPlatform.Communicate.Modbus;
using MCPlatform.Communicate.Modbus.Modbus;
using MCPlatform.MotionLib.Base.AssitantClass;
using MCPlatform.MotionLib.Base.Environment;
using MCPlatform.UI.Controls.Models;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MCPlatform.UI.Controls.Views.Communicate
{
    /// <summary>
    /// ModbusTcpControl.xaml 的交互逻辑
    /// </summary>
    public partial class ModbusTcpControl : UserControl
    {
        #region CTOR
        public ModbusTcpControl()
        {
            InitializeComponent();

           
        }

        #endregion

        #region Properties





        public ModbusTCPModel ModbusTCPModel
        {
            get { return (ModbusTCPModel)GetValue(ModbusTCPModelProperty); }
            set { SetValue(ModbusTCPModelProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ModbusTCPModel.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ModbusTCPModelProperty =
            DependencyProperty.Register("ModbusTCPModel", typeof(ModbusTCPModel), typeof(ModbusTcpControl), new PropertyMetadata(default));




        public TCPMaster? TCPMaster { get; set; }
        



        #endregion


        #region Methods
        private void btn_save_Click(object sender, RoutedEventArgs e)
        {
            String path = Paths.ModbusTcpConfigPaths;
            SaveConfigData.SaveConfig<ModbusTCPModel>(path, this.ModbusTCPModel);
        }

        private void btn_Debug_Click(object sender, RoutedEventArgs e)
        {
           if (this.TCPMaster is null || !this.TCPMaster.Connected)
            {
                Toast.Show("从站未连接！", targetWindow: ToastWindow.MainWindow);
                return;
            }

           new ModbusTcpDebugView(this.TCPMaster).Show();
        }

        private async void btn_Link_Click(object sender, RoutedEventArgs e)
        {
            if (TCPMaster is null)
            {
                Toast.Show("Modbus TCP为空！", targetWindow: ToastWindow.MainWindow);
                return;
            }
       

            if (TCPMaster.Connected)
            {
                TCPMaster.Disconnect();
            }
            else
            {
               var res = await TCPMaster.Connection();
                if (!res)
                {
                    Toast.Show("从站连接失败！", targetWindow: ToastWindow.MainWindow);
                }
            }
            SetUIDisable(TCPMaster.Connected);

        }

       
        private void SetUIDisable(bool enable)
        {
            //tb_IP.IsEnabled = enable;
            //tb_Port.IsEnabled = enable;
            //tb_SlaveAddr.IsEnabled = enable;
            //tb_SlaveName.IsEnabled = enable;
            //sw_AutoRetry.IsEnabled = enable;
        }

        #endregion

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            if (TCPMaster is null)
            {
                TCPMaster = this.ModbusTCPModel.TCPMaster;
            }
        }
    }
}
